﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using Public;

namespace QuanLyKhoHangVatTu
{
    public partial class ChiTietNhomVatTuHangHoa : DevExpress.XtraEditors.XtraForm,IChildForm
    {
        NhomVatTu_BUS nvt_bus = new NhomVatTu_BUS();
        NhomVatTu_PB nvt_pb = new NhomVatTu_PB();
        Flag_PB flag_pb = new Flag_PB();
        public ChiTietNhomVatTuHangHoa()
        {
            InitializeComponent();
        }

        private void ChiTietNhomVatTuHangHoa_Load(object sender, EventArgs e)
        {
            Nap();
        }

        public void Nap()
        {
            gridNhomVatTu.DataSource = nvt_bus.LayDSNhomVatTu();
        }
     
        private void gridNhomVatTu_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            
        }

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            NhomVatTu_PB.MaNhomVatTu = gridView1.GetRowCellValue(e.FocusedRowHandle, "MANHOMVATTU").ToString();
            NhomVatTu_PB.TenNhomVatTu = gridView1.GetRowCellValue(e.FocusedRowHandle, "TENNHOMVATTU").ToString();
        }
        public void Them()
        {
            NhomvatTuHangHoa nhomvthh = new NhomvatTuHangHoa();
            nhomvthh.Tag = this;
            nhomvthh.ShowDialog();
            Nap();
        }



        public void Sua()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;

            NhomvatTuHangHoa nhomvt = new NhomvatTuHangHoa(r["MANHOMVATTU"].ToString(), r["TENNHOMVATTU"].ToString());

            nhomvt.Tag = this;
            nhomvt.ShowDialog();
            Nap();
        }


        public void Xoa()
        {
            DataRow r = gridView1.GetFocusedDataRow();
            if (r == null) return;
            if (XtraMessageBox.Show(string.Format("Bạn có chắc muốn xóa Vật tư, hàng hóa <{0}>?"
                , r["MANHOMVATTU"]), "Xác nhận"
                 , MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
                return;
            try
            {
                nvt_bus.XoaNhomVatTu(r["MANHOMVATTU"].ToString());
            }
            catch (Exception ex)
            {
                if (ex.Message.Contains("conflicted with the REFERENCE constraint"))
                {
                    XtraMessageBox.Show("Không thể xóa vì có dữ liệu quan hệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            Nap();
        }
    }
}